﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 仓库用户权限查询
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WarehouseUserController : ControllerBase
    {
        private readonly ILogger<WMSWarehouseUserRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public WarehouseUserController(ILogger<WMSWarehouseUserRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("WMSDbConnection_ZH");

        }

        /// <summary>
        /// 仓库用户权限查询
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostWarehouseUserSelect([FromBody] WMSWarehouseUserRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            WMSWarehouseUserRequestDto result = new WMSWarehouseUserRequestDto();
            WMSWarehouseUserResponseDto re = new WMSWarehouseUserResponseDto();
            string warehouse_code = strparm.warehouse_code;
            string user_name = strparm.user_name;
            //框架查询方法
            //var data = _WMSTokenCheckService.GetTokenCheckData(supplier_code);

            try
            {
                strSql = new StringBuilder();
                strSql.Append(@"select u.user_name,u.nick_name,w.warehouse_code,w.warehouse_name from base_user_warehouse bu  
left join     sys_user u  on bu.user_id=u.user_id   left join base_warehouse w on bu.warehouse_id=w.id where 1=1 and u.status=0 and u.del_flag=0");

                if (warehouse_code.Trim()!="")
                {
                    strSql.Append(" and w.warehouse_code='" + warehouse_code.Trim() + "'");
                }
                if (user_name.Trim() != "")
                {
                    strSql.Append(" and u.user_name='" + user_name.Trim() + "'");
                }



                DataTable dt_barcodecheck = DB.Query(strSql.ToString(), con);
                if (dt_barcodecheck.Rows.Count<=0)
                {
                    strMsg = "用户《"+ user_name + "》没设置WMS仓库权限，请检查清楚再调用接口";
                }
                else
                {
                    re.data = DB.TableToListModel<WMSWarehouseUserResponseData>(dt_barcodecheck);
                }
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.status = "成功";
                re.code = "1";
                re.message = "";
            }
            else
            {
                re.status = "失败";
                re.code = "0";
                re.message = strMsg;
            }


            return new JsonResult(re);
        }





    }
}
